package com.vitas.coin.ui.fm

import android.view.View
import androidx.media3.common.Player
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.snap.base.util.BaseUtil
import com.snap.common.main.BaseMVVMFg
import com.vitas.coin.R
import com.vitas.coin.callback.MusicAdapterCallback
import com.vitas.coin.data.bean.ChildrenVoiceBean
import com.vitas.coin.data.event.CollectEvent
import com.vitas.coin.data.event.PausePlayEvent
import com.vitas.coin.data.event.StartPlayEvent
import com.vitas.coin.databinding.FrgHomeBinding
import com.vitas.coin.manager.PlayManager
import com.vitas.coin.ui.dialog.CollectListDialog
import com.vitas.coin.util.DisplayUtil
import com.vitas.coin.util.GlideRoundedCornersTransform
import com.vitas.coin.util.RenderViewOutlineProvider
import com.vitas.coin.vm.HomeVM
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.RectangleIndicator


class HomeFrg : BaseMVVMFg<FrgHomeBinding, HomeVM>(), View.OnClickListener {



    override fun createVM(): HomeVM {
        return HomeVM().apply {
            actionAct = { requireActivity() }
        }
    }

    override fun doDataBind() {
        binding.viewModel = viewModel
    }

    override fun getContentViewId(): Int {
        return R.layout.frg_home
    }

    override fun onViewCreated() {
        initEvent()
        initBottomPlay()
        initObserve()
        initBanner()
        viewModel.loadData(45L)
        initRv()
    }

    private fun initObserve() {
        BaseUtil.observer(this, StartPlayEvent::class.java) {
            if (binding.clBottom.visibility==View.GONE) {
                binding.clBottom.visibility = View.VISIBLE
            }
            viewModel.musicName.value = it.musicObj.musicName
            binding.ivPlay.setImageResource(R.mipmap.icon_pause)
            it.musicObj.id?.let { it1 -> viewModel.freshListByPlay(it1, true) }
        }

        BaseUtil.observer(this, PausePlayEvent::class.java) {
            binding.ivPlay.setImageResource(R.mipmap.icon_start)
            it.musicObj.id?.let { it1 -> viewModel.freshListByPlay(it1, false) }
        }

        BaseUtil.observer(this, CollectEvent::class.java) {
            viewModel.freshListByCollect(it.musicObj)
        }
    }

    private fun initBottomPlay() {
        val playStatus = PlayManager.getInstance().playStatus
        if (playStatus== Player.STATE_IDLE) {
            binding.clBottom.visibility = View.GONE
        }else{
            binding.clBottom.visibility = View.VISIBLE
            viewModel.musicName.value = PlayManager.getInstance().musicName
            if (PlayManager.getInstance().isPlaying) {
                binding.ivPlay.setImageResource(R.mipmap.icon_pause)
            }else{
                binding.ivPlay.setImageResource(R.mipmap.icon_start)
            }
        }
    }


    private fun initBanner() {
        binding.banner.outlineProvider =
            RenderViewOutlineProvider(DisplayUtil.dip2px(context,16f))
        binding.banner.clipToOutline = true
        binding.banner.setAdapter(object : BannerImageAdapter<Int?>(
            viewModel.bannerList as List<Int?>?
        ) {
            override fun onBindView(
                holder: BannerImageHolder,
                data: Int?,
                position: Int,
                size: Int
            ) {
                Glide.with(holder.itemView).load(data)
                    .apply(RequestOptions.bitmapTransform(
                        GlideRoundedCornersTransform(
                            context,
                            16f,
                            GlideRoundedCornersTransform.CornerType.ALL
                        )
                    ))
                    .into(holder.imageView)
            }

        }).addBannerLifecycleObserver(
            this
        ).setIndicator(RectangleIndicator(context))
    }



    private fun initEvent() {
        binding.ivShoucangList.setOnClickListener(this)
    }


    private fun initRv() {
        binding.mRecyclerView.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.mRecyclerView.adapter = viewModel.musicAdapter
        viewModel.musicAdapter.setDownloadCallBack(object :
            MusicAdapterCallback {
            override fun clickDownload(mChildrenVoiceBean: ChildrenVoiceBean?, type: Int) {
                if (mChildrenVoiceBean != null) {
                    viewModel.download(mChildrenVoiceBean,type)
                }
            }

            override fun clickCollect(mChildrenVoiceBean: ChildrenVoiceBean?) {
                if (mChildrenVoiceBean != null) {
                    viewModel.collect(mChildrenVoiceBean)
                }
            }
        })
        viewModel.musicAdapter.setType(1)
    }

    override fun onClick(p0: View?) {
        context?.let {
            CollectListDialog(it) {

            }.showXY(binding.clBottom)
        }
    }

}